<template>
  <div>
    <div>
      <div class="header flex">
        <el-button type="primary" @click="add()" icon="el-icon-plus">新增</el-button>
        <el-button type="danger" @click="clear()"  icon="el-icon-delete">删除</el-button>
        <!-- 弹框  新增-->
        <el-dialog title="新增" :visible.sync="dialogFormVisible">
  <el-form :model="form">
    <el-form-item label="部门名称" :label-width="formLabelWidth">
      <el-input v-model="form.deptName" placeholder="名称"></el-input>
    </el-form-item>
    <el-form-item label="上级部门" :label-width="formLabelWidth">
      <el-select v-model="form.lastDept" placeholder="请选择">
    <el-option
      v-for="item in options"
      :key="item.value"
      :label="item.label"
      :value="item.value">
    </el-option>
  </el-select>
    </el-form-item>
    <el-form-item label="排序号" :label-width="formLabelWidth">
      <el-input v-model="form.sortNo" placeholder="排序号"></el-input>
    </el-form-item>
  </el-form>
  <div slot="footer" class="dialog-footer">
    <el-button @click="dialogFormVisible = false">取 消</el-button>
    <el-button type="primary" @click="submitForm(form)">确 定</el-button>
  </div>
</el-dialog>
<el-dialog title="修改" :visible.sync="dialogFormVisible2">
  <el-form :model="form2">
    <el-form-item label="部门名称" :label-width="formLabelWidth">
      <el-input v-model="form2.deptName" placeholder="名称"></el-input>
    </el-form-item>
    <el-form-item label="上级部门" :label-width="formLabelWidth">
      <el-select v-model="form2.lastDept" placeholder="请选择">
    <el-option
      v-for="item in options"
      :key="item.value"
      :label="item.label"
      :value="item.value">
    </el-option>
  </el-select>
    </el-form-item>
    <el-form-item label="排序号" :label-width="formLabelWidth">
      <el-input v-model="form2.sortNo" placeholder="排序号"></el-input>
    </el-form-item>
  </el-form>
  <div slot="footer" class="dialog-footer">
    <el-button @click="dialogFormVisible = false">取 消</el-button>
    <el-button type="primary" @click="submitForm2(form2)">确 定</el-button>
  </div>
</el-dialog>
      </div>

      <!-- 下面表格 -->
      <div style="width:90%;margin: auto;"></div>
      <el-table
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%"
        :header-cell-style="{background:'#eef1f6',color:'#606266'}"
        @selection-change="handleSelectionChange">
      <!-- 单选全选 点击按钮 -->
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column label="部门ID" width="200">
          <template slot-scope="scope">{{ scope.row.id }}</template>
        </el-table-column>
        <el-table-column prop="name" label="部门名称" width="280">
          <template slot-scope="scope">{{ scope.row.deptName }}</template>
        </el-table-column>
        <el-table-column prop="address" label="上级部门" width="280">
          <template slot-scope="scope">{{ scope.row.lastDept }}</template>
        </el-table-column>
        <el-table-column label="排序号" width="280">
          <template slot-scope="scope">{{ scope.row.sortNo }}</template>
    </el-table-column>
    <el-table-column label="操作">
      <template slot-scope="scope">
        <el-button
          size="60"
          @click="handleEdit(scope.row)" type="warning" icon="el-icon-edit">修改</el-button>
      </template>
      </el-table-column>
      </el-table>
      <Pagin :count="total" @pageEvent="fetchData" :pageSize="pageSize"></Pagin>
    </div>
  </div>
</template>

<script>
import {staListAPI,addStaInsertAPI,staUpdateAPI,staDeleteAPI,depListAPI} from '../../api/dv'
export default {
  data() {
    return {
      options:[],
      show:false,
      form: {
        deptName: '',
        lastDept: '',
        sortNo:''
        },
        form2: {
        deptName: '',
        lastDept: '',
        sortNo:''
        },
      formLabelWidth:'120px',
      type:'danger',
      dialogFormVisible: false,
      dialogFormVisible2: false,
      dataList:[],
      total: 0, // 列表总行数
        currentPage: 1,
        pageSize: 10,
      tableData: [],
      multipleSelection: [],
      user:'',
      defaultProps: {
          children: 'children',
          label: 'label'
        }
    };
  },
  created(){
    this.jia()
    this.bumen()
  },
  components: {
    Pagin: () => import('c/Pagin')
  },
  methods: {
    handleSelectionChange(val) {
      console.log(val);
      this.multipleSelection = val;
    },
   async submitForm(form) {
    const res = await addStaInsertAPI({
        deptName : form.deptName , //主题名
        lastDept:  form.lastDept,//开始时间
        sortNo:  form.sortNo,//内容
    })
        //请求成功后
        if(res.code == '1'){
        this.jia()
        this.$message.success('添加成功')
       }else{
        this.$message.error('添加失败')
       }
      //关闭
      this.dialogFormVisible = false
    },
    //删除
   async clear(){
      var id = []
      this.multipleSelection.forEach(item=>{
        // console.log(item.id,'sda')
         id.push(item.id)
      })
      //请求成功后
      if(id.length > 0){
        const res = await staDeleteAPI(id)
         //请求成功后
         if(res.code == '1'){
        this.jia()
        this.$message.success('删除成功')
       }else{
        this.$message.error('删除失败')
       }
      }
    },
    handleEdit(row){
      // console.log(row);
      this.dialogFormVisible2 = true
      this.form2  = row

    },
    //修改确定
   async submitForm2(form){
      this.dialogFormVisible2 = false
      const res  = await staUpdateAPI({
        deptName : form.deptName , //主题名
        lastDept:  form.lastDept,//开始时间
        sortNo:  form.sortNo,//内容
        id : form.id
      })
      if(res.code == '1'){
        this.jia()
        this.$message.success('修改成功')
       }else{
        this.$message.error('修改失败')
       }
    },
      //新增
      add(){
      this.dialogFormVisible = true
    },
     //获取部门
     async bumen(){
      const res2 = await depListAPI()
      // console.log(res2);
      var arr = []
      res2.data.forEach(item=>{
        var obj = {value:item,label:item}
        arr.push(obj)

      })
      this.options = arr
    },
    //模拟假数据
   async jia(){
    const res = await staListAPI()
      this.dataList = res.data
      this.total = this.dataList.length
      this.fetchData(this.currentPage)
    },
    fetchData (count) {
      var list = this.dataList
      var from = (count - 1) * this.pageSize
      const to = count * this.pageSize
      this.tableData = []
      for (; from < to; from++) {
        if (list[from]) {
          this.tableData.push(list[from])
        }
      }
    },
  },
};
</script>
<style scoped lang="scss">
.flex {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.input {
  width: 120px;
}
.header {
  width: 20%;
  // border-bottom: 1px solid #e0e0e0;
  padding: 20px 0;
}

</style>
